//
//维修工单
//
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column
            label="维修图片"
            min-width="120"
          >
            <template slot-scope="scope">
              <FileManage
                :file-names="scope.row.fileName"
                :file-real-names="scope.row.fileRealName"
                :file-urls="scope.row.fileUrl"
                :readonly="true"
                class="accessory"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status !== 3"
                v-hasPermi="['device:repairRecord:operate']"
                type="text"
                @click.stop="edit(scope.row,scope.row.isEdit=true)"
              >维修</el-button>
              <span v-else class="el-button--text button_grey">维修</span>
              <el-button
                v-if="scope.row.status == 3"
                v-hasPermi="['device:repairRecord:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, getInfo } from '@/api/equipmentManage/equipmentRepair/repairRecord'
import { mapGetters } from 'vuex'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import ShowRecord from './ShowRecord'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo' // 查看页面

export default {
  name: 'Index',
  computed: {
    ...mapGetters([
      'userId', 'userName', 'notificationId', 'notificationStatus', 'repairRecordStatus'
    ])
  },
  data() {
    return {
      loading: true,
      pageId: '维修工单', // 页面唯一标识
      pageExportId: '维修工单导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'code',
        isAsc: 'desc',
        selectMyselfId: ''
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      pushColumns: [], // 推送配置实际用的字段参数
      selectionsAll: [], // 表格所有页选中的行
      labelData: {}, // 查看数据
      labelList: [],
      pushUserList: [],
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '维修单号', sortable: 'custom' },
        { prop: 'reportCode', label: '报修单号' },
        { prop: 'deviceStandingBookCodeName', label: '报修设备', minWidth: '200', canOrder: false },
        { prop: 'deviceStandingBookLocation', label: '设备位置', canOrder: false },
        { prop: 'repairPersonName', label: '维修人员', canOrder: false },
        { prop: 'planTime', label: '计划修复时间范围', minWidth: '280', canOrder: false },
        { prop: 'completeTime', label: '维修完成时间', minWidth: '180', canOrder: false },
        { prop: 'malfunctionTypeName', label: '故障类型', canOrder: false },
        { prop: 'repairOptionName', label: '维修类型', canOrder: false },
        { prop: 'firmName', label: '维修厂商', canOrder: false },
        { prop: 'cost', label: '维修费用(元)', canOrder: false },
        { prop: 'duration', label: '维修时长(h)', canOrder: false },
        { prop: 'description', label: '维修纪要', canOrder: false },
        { prop: 'tempSolution', label: '临时处理对策', canOrder: false },
        { prop: 'longTermSolution', label: '长期改善对策', canOrder: false }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '维修单号', sortable: 'custom' },
        { prop: 'reportCode', label: '报修单号' },
        { prop: 'deviceStandingBookCodeName', label: '报修设备', minWidth: '200', canOrder: false },
        { prop: 'deviceStandingBookLocation', label: '设备位置', canOrder: false },
        { prop: 'repairPersonName', label: '维修人员', canOrder: false },
        { prop: 'planTime', label: '计划修复时间范围', minWidth: '280', canOrder: false },
        { prop: 'completeTime', label: '维修完成时间', minWidth: '180', canOrder: false },
        { prop: 'malfunctionTypeName', label: '故障类型', canOrder: false },
        { prop: 'repairOptionName', label: '维修类型', canOrder: false },
        { prop: 'firmName', label: '维修厂商', canOrder: false },
        { prop: 'cost', label: '维修费用(元)', canOrder: false },
        { prop: 'duration', label: '维修时长(h)', canOrder: false },
        { prop: 'description', label: '维修纪要', canOrder: false },
        { prop: 'tempSolution', label: '临时处理对策', canOrder: false },
        { prop: 'longTermSolution', label: '长期改善对策', canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'reportCode', label: '报修单号', type: 'input' },
        // { prop: 'code', label: '维修单号', type: 'input' },
        { prop: 'deviceStandingBookId', label: '报修设备', type: 'select', optionsName: 'repairs' },
        // { prop: 'deviceName', label: '设备名称', type: 'input' },
        // { prop: 'repairPersonName', label: '维修人员', type: 'input' },
        // { prop: 'status', label: '状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'statusPc', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' },
        // { prop: 'deviceStandingBookLocation', label: '设备位置', type: 'input' },
        // { prop: 'malfunctionTypeName', label: '故障类型', type: 'input' },
        { prop: 'repairOption', label: '维修类型', type: 'select', optionsName: 'repairOption' },
        // { prop: 'firmName', label: '维修厂商', type: 'input' },
        // { prop: 'duration', label: '维修时长', type: 'input' },
        { prop: 'completeTime', label: '维修完成时间', type: 'time' }
      ],
      defaultPushColumns: [// 推送配置常规字段（初始值）
        { prop: 'pushCycle', label: '推送周期', type: 'select', optionsName: 'pushCycleList' },
        { prop: 'sendUsers', label: '推送对象', type: 'pushUser-select', optionsName: 'pushUserList' },
        { prop: 'reportCode', label: '报修单号', type: 'input' },
        { prop: 'code', label: '维修单号', type: 'input' },
        { prop: 'deviceStandingBookId', label: '报修设备', type: 'select', optionsName: 'repairs' },
        { prop: 'repairPersonName', label: '维修人员', type: 'input' },
        // { prop: 'status', label: '状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'statusPc', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' },
        { prop: 'deviceStandingBookLocation', label: '设备位置', type: 'input' },
        { prop: 'malfunctionTypeName', label: '故障类型', type: 'input' },
        { prop: 'repairOption', label: '维修类型', type: 'select', optionsName: 'repairOption' },
        { prop: 'firmName', label: '维修厂商', type: 'input' },
        { prop: 'duration', label: '维修时长', type: 'input' },
        { prop: 'completeTime', label: '维修完成时间', type: 'time-select' }
      ],
      repairOption: [
        { label: '全部', value: undefined },
        { label: '自修', value: '0' },
        { label: '委外', value: '1' }
      ],
      pushCycleList: [
        { value: 1, label: '每天' },
        { value: 2, label: '每周一' },
        { value: 3, label: '每月一号' }
      ],
      statusOptions: [ // 选择组件数据源
        // { value: undefined, label: '全部' },
        { value: 0, label: '待维修' },
        { value: 4, label: '维修中' },
        { value: 3, label: '已完成' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '打印', perm: '' },
        { label: '批量导出3', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      repairs: [],
      applyDevice: [],
      showKey: [ // 查看详情的配置（根据配置自动生成查看页面）
        {
          name: '设备维修',
          columns: [
            { type: 'form', name: '维修单号', key: 'recoedCode' }, // type: 'form' 表示是普通的表单展示
            { type: 'form', name: '故障类型', key: 'malfunctionTypeName' },
            { type: 'form', name: '维修类型', key: 'repairOptionName' },
            { type: 'form', name: '维修厂商', key: 'repairFirmName' },
            { type: 'form', name: '维修费用(元)', key: 'cost' }, // length:'double' 表示占两列
            { type: 'form', name: '维修时长(h)', key: 'duration' },
            { type: 'form', name: '维修完成时间', key: 'completeTime' },
            { type: 'file', name: '维修图片', fileName: 'maintainFileName', fileRealName: 'maintainFileRealName', fileUrl: 'maintainFileUrl' },
            { type: 'form', name: '维修纪要', key: 'description', length: 'double' },
            { type: 'form', name: '临时处理对策', key: 'tempSolution', length: 'double' },
            { type: 'form', name: '长期改善对策', key: 'longTermSolution', length: 'double' },
            {
              type: 'table', key: 'labelList', columns: [ // type: 'table' 表示是表格
                { type: 'column', name: '备件编码', key: 'spareCode', minWidth: '120' },
                { name: '备件名称', key: 'spareName', minWidth: '100' },
                { name: '规格型号', key: 'spareModel', minWidth: '100' },
                { name: '备件单位', key: 'numberUnitLabel', minWidth: '100' },
                { name: '仓库', key: 'deviceSpareWarehouseName', minWidth: '100' },
                { name: '批号', key: 'batch', minWidth: '100' },
                { name: '库存数量', key: 'inventoryNumber', minWidth: this.SETTINGS.settings_decimalDigits_length },
                { name: '使用数量', key: 'number', minWidth: this.SETTINGS.settings_decimalDigits_length }
              ]
            }
          ]
        },
        {
          name: '设备信息',
          columns: [
            { type: 'form', name: '设备编码', key: 'facilityCode' }, // type: 'form' 表示是普通的表单展示
            { type: 'form', name: '设备名称', key: 'name' },
            { type: 'form', name: '设备类型', key: 'deviceTypeName' },
            { type: 'form', name: '规格型号', key: 'model' },
            { type: 'form', name: '设备厂商', key: 'firmName' }
          ]
        },
        {
          name: '报修信息',
          columns: [
            { type: 'form', name: '报修单号', key: 'repairsReportCode' }, // type: 'form' 表示是普通的表单展示
            { type: 'form', name: '报修设备', key: 'bookName' },
            { type: 'form', name: '设备状态', key: 'deviceStatusLabel' },
            { type: 'form', name: '设备故障时间', key: 'malfunctionTime' },
            { type: 'form', name: '期望修复时间', key: 'planEndTime' },
            { type: 'form', name: '故障类型', key: 'typeName' },
            { type: 'form', name: '紧急程度', key: 'emergencyDegreeName' },
            { type: 'form', name: '报修人员', key: 'reportPersonName' },
            { type: 'file', name: '故障情况拍照', fileName: 'docName', fileRealName: 'docRealName', fileUrl: 'docUrl' },
            { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
            { type: 'form', name: '故障现象', key: 'phenomenon', length: 'double' },
            { type: 'form', name: '维修人员', key: 'repairPersonName' },
            { type: 'form', name: '计划修复时间范围', key: 'planTime', length: 'double' }
          ]
        }
      ]
    }
  },
  watch: {
    notificationId(newValue, oldValue) {
      if (newValue !== oldValue && newValue !== 0 && this.$store.state.notification.notificationStatus == '0') {
        const rowId = { id: this.notificationId }
        this.edit(rowId)
      } else if (newValue !== oldValue && newValue !== 0 && this.$store.state.notification.notificationStatus == '1') {
        const rowId = { id: this.notificationId }
        this.ShowRecord(rowId)
      }
    }
  },
  mounted() {
    // if (this.$store.state.common.routeData) {
    //   const routeData = this.$deepClone(this.$store.state.common.routeData)
    //   const cachePageData = this.$deepClone(this.parent.pageData)
    //   this.$store.state.common.routeData = '' // 用完及时清除
    //   Object.keys(routeData).forEach(key => {
    //     cachePageData[key] = routeData[key]
    //   })
    //   this.parent.pageData = cachePageData
    // }
    // this.statusOptions = [ // 选择组件数据源
    //   { value: 0, label: '待维修' },
    //   { value: 4, label: '维修中' },
    //   { value: 3, label: '已完成' }
    // ]
    // if (this.repairRecordStatus) {
    //   console.log('从首页跳转')
    //   this.statusOptions = [ // 选择组件数据源
    //     { value: 0, label: '待维修' },
    //     { value: 4, label: '维修中' }
    //   ]
    //   this.$store.dispatch('notification/repairRecordStatus', '')
    // }
    if (this.notificationId !== 0) {
      const rowId = { id: this.notificationId }
      // console.log(this.$store.state)
      if (this.$store.state.notification.notificationStatus == '0') {
        this.edit(rowId)
      } else {
        this.ShowRecord(rowId)
      }
    }
    if (this.$route.params.id) {
      this.edit(this.$route)
    }
    this.getName()
  },
  created() {
    if (this.$store.state.order.contendId) {
      if (this.$store.state.order.isStatus == '1') {
        this.ShowRecord({ id: this.$store.state.order.contendId })
      } else {
        this.edit({ id: this.$store.state.order.contendId, isEdit: true })
      }
      this.$store.state.order.contendId = ''
      this.$store.state.order.isStatus = ''
    }
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.ShowRecord(row)
          break
      }
    },
    getList() {
      this.pageData.selectMyselfId = this.userId
      list(this.pageData).then(res => {
        res.data.forEach(item => {
        })
        this.tableData = res.data
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getName() {
      getEquipmentList1().then(res => {
        const cacheData = []
        res.data.forEach(item => {
          cacheData.push({
            value: item.id,
            label: item.deviceName
          })
        })
        this.repairs = cacheData
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            isEdit: row.isEdit || ''
          }// props
        },
        area: ['1320px', '700px'],
        title: '执行维修任务', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
      this.$store.dispatch('notification/changeId', 0)
    },
    ShowRecord(row) {
      getInfo(row.id).then(res => {
        // 此处展示的页面有是否默认字段，后端给的是1，前端处理成 是
        if (res.data.data) {
          res.data.data.forEach(item => { item.isDefault == '1' ? item.isDefault = '是' : item.isDefault = '' })
        }
        this.labelList = res.data[1]
        const completeTime1 = res.data[0].completeTime
        const recoedCode = res.data[0].code
        const list = {
          ...res.data[0],
          ...res.data[2],
          ...res.data[3]
        }
        list.completeTime = completeTime1
        list.recoedCode = recoedCode
        this.labelData = list
        this.$set(this.labelData, 'labelList', this.labelList)
        this.$layer.iframe({// 弹框
          content: {
            content: ShowRecord, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: this.labelData,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看维修工单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
      this.$store.dispatch('notification/changeId', 0)
    },
    getExportData(data) {
      return list(data)
    }
  }
}
</script>

<style lang="less" scoped>
.accessory {
  color: #387bfc
}
</style>

